#########################################
# This script combines the different 
# expert surveys used in the analysis
#########################################

cat("#######################################")
cat("\nStarting to combine expert surveys.\n")
cat("#######################################\n")

container <- map(paste0(c("gps", "poppa", "ches"),
                        "_prep.R"),
                 source)

df <- dplyr::inner_join(poppa_prep, ches_prep, by = "id_ches") %>% 
  mutate(lrecon_salience3 = factor(ifelse(lrecon_salience_ches < quantile(lrecon_salience_ches, .33, na.rm=T), "Economic Salience: Low",
                                          ifelse(lrecon_salience_ches < quantile(lrecon_salience_ches, .66, na.rm=T), "Economic Salience: Medium",
                                                 ifelse(lrecon_salience_ches >= quantile(lrecon_salience_ches, .66, na.rm=T), "Economic Salience: High", NA))),
                                   levels = c("Economic Salience: Low", "Economic Salience: Medium", "Economic Salience: High")),
         galtan_salience3 = factor(ifelse(galtan_salience_ches < quantile(galtan_salience_ches, .33, na.rm=T), "Cultural Salience: Low",
                                          ifelse(galtan_salience_ches < quantile(galtan_salience_ches, .66, na.rm=T), "Cultural Salience: Medium",
                                                 ifelse(galtan_salience_ches >= quantile(galtan_salience_ches, .66, na.rm=T), "Cultural Salience: High", NA))),
                                   levels = c("Cultural Salience: Low", "Cultural Salience: Medium", "Cultural Salience: High")),
         lrecon_poppa_raw = lrecon_poppa,
         lrecon_poppa = (lrecon_poppa - mean(lrecon_poppa, na.rm = T)) / sd(lrecon_poppa, na.rm = T),
         lrecon_ches = (lrecon_ches - mean(lrecon_ches, na.rm = T)) / sd(lrecon_ches, na.rm = T),
         galtan_ches = (galtan_ches - mean(galtan_ches, na.rm = T)) / sd(galtan_ches, na.rm = T),
         immigration_poppa = (immigration_poppa - mean(immigration_poppa, na.rm = T)) / -sd(immigration_poppa, na.rm=T),
         nativism_poppa = (nativism_poppa - mean(nativism_poppa, na.rm = T)) / sd(nativism_poppa, na.rm=T),
         laworder_poppa = (laworder_poppa - mean(laworder_poppa, na.rm = T)) / sd(laworder_poppa, na.rm=T),
         lifestyle_poppa = (lifestyle_poppa - mean(lifestyle_poppa, na.rm = T)) / -sd(lifestyle_poppa, na.rm=T),
         diff_salience = lrecon_salience_ches - galtan_salience_ches,
         comp_salience =  factor(cut(diff_salience,
                                     quantile(diff_salience, 
                                              seq(0, 1, length.out = 4), 
                                              na.rm = T)),
                                 label = c("Salience:\nCulture focus",
                                           "Salience:\nBalanced focus",
                                           "Salience:\nEconomic focus")),
         comp_salience = factor(gtools::quantcut(diff_salience,
                                                 q = 3),
                                labels = c("Salience:\nCulture focus",
                                           "Salience:\nBalanced focus",
                                           "Salience:\nEconomic focus")),
         party_fam_3 =  case_when(partyfamily_poppa == "Radical Right" ~ "Radical Right",
                                  partyfamily_poppa == "Radical Left" ~ "Radical Left",
                                  TRUE ~ "Other"),
         lrecon_salience3 = factor(gtools::quantcut(lrecon_salience_ches,
                                                    q = 3),
                                   labels = c("Economic Salience: Low",
                                              "Economic Salience: Medium",
                                              "Economic Salience: High")),
         galtan_salience3 = factor(gtools::quantcut(galtan_salience_ches,
                                                    q = 3),
                                   labels = c("Cultural Salience: Low",
                                              "Cultural Salience: Medium",
                                              "Cultural Salience: High")),
         country_long = countrycode::countrycode(ifelse(grepl("BE-", country_poppa), "BE",
                                                        ifelse(country_poppa == "CR", "HR", 
                                                               ifelse(country_poppa == "GE", "DE",
                                                                      ifelse(country_poppa == "BU", "BG", 
                                                                             ifelse(country_poppa == "IR", "IE", 
                                                                                    ifelse(country_poppa == "MA", "MT", 
                                                                                           ifelse(country_poppa == "UK", "GB", 
                                                                                                  ifelse(country_poppa == "ES", "EE", 
                                                                                                         ifelse(country_poppa == "SP", "ES", 
                                                                                                                ifelse(country_poppa == "AU", "AT", country_poppa)))))))))), origin = "iso2c", destination = "iso3c"))



cat("\n#######################################")
cat("\nData combine finished.\n")
cat("#######################################\n")
